<?php

class Usermail
{
	public function test()
	{
		require(APPPATH.'/libraries/class.phpmailer.php');
		if(ci()->config->item('email_smtp_host') != ''

			and ci()->config->item('email_smtp_port') != ''

			and ci()->config->item('email_smtp_username') != ''

			and ci()->config->item('email_smtp_password') != '' ) 

		{
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = 'ssl';
			$mail->Host = ci()->config->item('email_smtp_host');
			$mail->Username = ci()->config->item('email_smtp_username');
			$mail->Password = ci()->config->item('email_smtp_password');
			$mail->Port = ci()->config->item('email_smtp_port');
			$mail->FromName = "Treasury";
			$mail->From = 'admin@domain.com';
			$mail->Subject = 'Hello Long';
			$mail->MsgHTML('Test');
			$mail->AddAddress('lnguyen.info@gmail.com');

			try {
				$mail->Send();

				return true;

			}

			catch(Exception $e) {

				return false;

			}
		}


	}


	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function sendmail($args = array())

	{

		//ci()->load->library('email');
		require('class.phpmailer.php');



		if(ci()->config->item('email_smtp_host') != ''

			and ci()->config->item('email_smtp_port') != ''

			and ci()->config->item('email_smtp_username') != ''

			and ci()->config->item('email_smtp_password') != '' ) 

		{
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = 'ssl';
			$mail->Host = ci()->config->item('email_smtp_host');
			$mail->Username = ci()->config->item('email_smtp_username');
			$mail->Password = ci()->config->item('email_smtp_password');
			$mail->Port = ci()->config->item('email_smtp_port');
			$mail->FromName = "Treasury";
			$mail->From = 'admin@domain.com';
			$mail->Subject = ci()->config->item('message_register_subject');
			$mail->MsgHTML(str_replace(array(
					'{username}', '{password}', '{site_url}', '{email}'
				), array(
					$args['username'], $args['password'], site_url(), $args['email']
				), ci()->config->item('message_register_content')));
			$mail->AddAddress($args['email']);

			try {
				$mail->Send();

				return true;

			}

			catch(Exception $e) {

				return false;

			}
		}
		
		return false;

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function send_mail_forgot_password($email, $username, $key)
	{
		require('class.phpmailer.php');
		if(ci()->config->item('email_smtp_host') !== ''
			and ci()->config->item('email_smtp_port') !== ''
			and ci()->config->item('email_smtp_username') !== ''
			and ci()->config->item('email_smtp_password') !== '' ) 
		{
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = 'ssl';
			$mail->Host = ci()->config->item('email_smtp_host');
			$mail->Username = ci()->config->item('email_smtp_username');
			$mail->Password = ci()->config->item('email_smtp_password');
			$mail->Port = ci()->config->item('email_smtp_port');
			$mail->FromName = "Treasury";
			$mail->From = 'admin@domain.com';
			$mail->Subject = 'Request reset password';
			$body = ci()->load->view('auth/email/forgot_password', array(
				'identity' => $username,
				'forgotten_password_code' => $key
			), true);
			$mail->MsgHTML($body);
			$mail->AddAddress($email);
			
			try {
				$mail->Send();
				return true;
			}
			catch(Exception $e) {
				return false;
			}
		}

		return false;

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function send_mail_forgot_password_success($email, $username, $password)

	{
	
		require('class.phpmailer.php');


		if(ci()->config->item('email_smtp_host') !== ''
			and ci()->config->item('email_smtp_port') !== ''
			and ci()->config->item('email_smtp_username') !== ''
			and ci()->config->item('email_smtp_password') !== '' ) 
		{
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = 'ssl';
			$mail->Host = ci()->config->item('email_smtp_host');
			$mail->Username = ci()->config->item('email_smtp_username');
			$mail->Password = ci()->config->item('email_smtp_password');
			$mail->Port = ci()->config->item('email_smtp_port');
			$mail->FromName = "Treasury";
			$mail->From = 'admin@domain.com';
			$mail->Subject = 'Your new password - Treasury';
			$body = ci()->load->view('auth/email/new_passsword', array(
				'identity' => $username,
				'new_password' => $password
			), true);
			$mail->MsgHTML($body);
			$mail->AddAddress($email);
			
			try {
				$mail->Send();
				return true;
			}

			catch(Exception $e) {
				return false;
			}
		}

	}
}